library(ggplot2)
library(readxl)
library(zoo)
library(grid)
library(gridExtra)

windowsFonts(Times=windowsFont("TT Times New Roman"))
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

rm(list=ls(all=TRUE))

# Different date of covid #
case <- read.delim("diffdate_case.txt")
death <- read.delim("diffdate_death.txt")

case$date <- as.Date(as.character(case$c5), format = "%Y%m%d")
case$coefficient <- as.numeric(case$c1)
case$lower <- as.numeric(case$c2)
case$upper <- as.numeric(case$c3)

death$date <- as.Date(as.character(case$c5), format = "%Y%m%d")
death$coefficient <- as.numeric(death$c1)
death$lower <- as.numeric(death$c2)
death$upper <- as.numeric(death$c3)


p1<-ggplot(case,aes(x = date, y= coefficient)) +
  geom_ribbon(aes(ymin = lower, ymax=upper), alpha = 0.2)+
  geom_line()+
  geom_hline(yintercept=0, linetype='dashed', color='tomato', size=1)+
  ylim(-1.5,1)+
  scale_x_date(date_breaks = "2 month", date_minor_breaks = "1 month", 
               date_labels = "%B", limits=c(as.Date("2020-03-11"), as.Date("2021-01-01")))+
  labs(y="Coefficient", x="") +
  theme_bw()+
  theme(text=element_text(family="Times"), axis.title.x=element_text(size=15, colour = "black"),
        axis.text.x=element_text(size=13, colour = "black"),
        axis.title.y = element_text(size=15, colour = "black"),
        axis.text.y = element_text(size=13, colour = "black"))

p1
pdf("FigureA8a.pdf", width=6, height=4)
print(p1)
dev.off()

p2<-ggplot(death,aes(x = date, y=coefficient)) +
  geom_ribbon(aes(ymin = lower, ymax=upper), alpha = 0.2) +
  ylim(-1.5,1)+
  scale_x_date(date_breaks = "2 month", date_minor_breaks = "1 month", 
               date_labels = "%B", limits=c(as.Date("2020-03-11"), as.Date("2021-01-01")))+
  geom_line()+
  geom_hline(yintercept=0, linetype='dashed', color='tomato', size=1)+
  labs(y="Coefficient", x="") +
  theme_bw()+
  theme(text=element_text(family="Times"), axis.title.x=element_text(size=15, colour = "black"),
        axis.text.x=element_text(size=13, colour = "black"),
        axis.title.y = element_text(size=15, colour = "black"),
        axis.text.y = element_text(size=13, colour = "black"))
p2
pdf("FigureA8b.pdf", width=6, height=4)
print(p2)
dev.off()

# Different year of political trust #
case <- read.delim("diffyear_case.txt")
death <- read.delim("diffyear_death.txt")

case$Year <- seq(2009,2018,by = 1)
case$coefficient <- as.numeric(case$c1)
case$lower <- as.numeric(case$c2)
case$upper <- as.numeric(case$c3)

death$Year <- seq(2009,2018,by = 1)
death$coefficient <- as.numeric(death$c1)
death$lower <- as.numeric(death$c2)
death$upper <- as.numeric(death$c3)

p1<-ggplot(case,aes(x = Year, y= coefficient)) +
  geom_ribbon(aes(ymin = lower, ymax= upper), alpha = 0.2)+
  geom_line()+
  ylim(-1.2, 0.2)+
  geom_hline(yintercept=0, linetype='dashed', color='tomato', size=1)+
  labs(y="Coefficient", x="") +
  theme_bw()+
  theme(text=element_text(family="Times"), axis.title.x=element_text(size=14, colour = "black"),
        axis.text.x=element_text(size=12, colour = "black"),
        axis.title.y = element_text(size=14, colour = "black"),
        axis.text.y = element_text(size=12, colour = "black"))
p1
pdf("FigureA9a.pdf", width=6, height=4)
print(p1)
dev.off()


p2<-ggplot(death,aes(x = Year, y= coefficient)) +
  geom_ribbon(aes(ymin = lower, ymax= upper), alpha = 0.2)+
  geom_line()+
  ylim(-1.2, 0.2)+
  geom_hline(yintercept=0, linetype='dashed', color='tomato', size=1)+
  labs(y="Coefficient", x="") +
  theme_bw()+
  theme(text=element_text(family="Times"), axis.title.x=element_text(size=14, colour = "black"),
        axis.text.x=element_text(size=12, colour = "black"),
        axis.title.y = element_text(size=14, colour = "black"),
        axis.text.y = element_text(size=12, colour = "black"))
p2
pdf("FigureA9b.pdf", width=6, height=4)
print(p2)
dev.off()